package nav;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Login
 */
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
        super();

    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			// no se hace nada con el get.
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String user=(String)request.getParameter("user");
		String pass=(String)request.getParameter("pass");
		System.out.println(user+" "+ pass);
		
		Integer loginerr=0;
		if( request.getSession().getAttribute("loginerr")!=null)
			loginerr=(Integer)request.getSession().getAttribute("loginerr");
			
		//TODO: Validar el cliente contra el sistema.
		if(user.equals("pepe") && pass.equals("pepe"))
		{
			//Seteo el usuario como logueado.
			request.getSession().setAttribute("user", user);
			request.getSession().setAttribute("logged", true);
			response.sendRedirect(PagesNav.INDEX);
		}
		else
		{
			request.getSession().setAttribute("logged", false);
			loginerr++;
			request.getSession().setAttribute("loginerr", loginerr );
			response.sendRedirect(PagesNav.LOGIN);
		}
	}

}
